<template>
  <div>
    <router-link tag="div" to="/" class="header-back" v-show="showHeaderBack">
      <span class="iconfont icon-back">&#xe624;</span>
    </router-link>
    <div class="header" :style="opacityStyle" v-show="!showHeaderBack">
      <router-link to="/" class="header-left">
        <span class="iconfont header-left-back">&#xe624;</span>
      </router-link>
      XXXXXX
    </div>
  </div>
</template>

<script>
export default {
  name: "DetailHeader",
  data: function () {
    return {
      showHeaderBack: true,
      opacityStyle: {
        opacity: 0,
      },
    };
  },
  //keep-alive生命周期函数
  activated: function () {
    window.addEventListener("scroll", this.handleScroll);
  },
  deactivated: function () {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    handleScroll: function () {
      var top = document.documentElement.scrollTop;
      if (top > 50) {
        var opacity = top / 100;
        opacity = opacity > 1 ? 1 : opacity;
        this.opacityStyle = {
          opacity: opacity,
        };
        this.showHeaderBack = false;
      } else {
        this.showHeaderBack = true;
      }
    },
  },
};
</script>

<style lang="stylus" scoped>
@import '~styles/variables.styl';

.header-back {
  position: absolute;
  top: 0.2rem;
  left: 0.2rem;
  width: 0.8rem;
  height: 0.8rem;
  line-height: 0.8rem;
  text-align: center;
  border-radius: 0.4rem;
  background-color: rgba(0, 0, 0, 0.5);
}

.header-back .icon-back {
  color: #fff;
  font-size: 0.4rem;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 0.88rem;
  line-height: 0.88rem;
  text-align: center;
  background-color: $bgColor;
  color: #fff;
  font-size: 0.32rem;
}

.header .header-left {
  position: absolute;
  top: 0;
  left: 0;
  display: inline-block;
  width: 0.8rem;
  height: 100%;
  color: #fff;
}
</style>